package com.besall.allbase.view.activity.chipstoollevel4.protocol.FitnessParams;

import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_ERASE_DATE_FITNESS;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_ERASE_DATE_VALUE;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SPO2;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;

public class EraseFitnessData extends DialogFragment {
        public int v = 0;
        public int days = 0;
    private ConfigCallBack mConfigCallback;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.config_erasefitness, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){

        RadioGroup chooseFormat = (RadioGroup) view.findViewById(R.id.fitness);
        chooseFormat.setOnCheckedChangeListener(FormatCheckChangeListener);

        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);
        v = 0;

        LoopView date = (LoopView) view.findViewById(R.id.date);
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            list.add("" + i);
        }
        date.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                days = index;
            }
        });
        date.setItems(list);

        date.setInitPosition(0);
    }

    public boolean saveConfig(){
        SPHelper.putPreference(getActivity(), KEY_ERASE_DATE_VALUE, days);
        SPHelper.putPreference(getActivity(), KEY_ERASE_DATE_FITNESS, v);
        return true;
    }

    private final RadioGroup.OnCheckedChangeListener FormatCheckChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if(checkedId == R.id.step){
                v = 0;
            }else if(checkedId == R.id.sleep){
                v = 1;
            }else if(checkedId == R.id.sport){
                v = 2;
            }

/*            switch (checkedId){
                case R.id.step:
                    v = 0;
                    break;
                case R.id.sleep:
                    v = 1;
                    break;
                case R.id.sport:
                    v = 2;
                default:
                    break;
            }*/
        }
    };


    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }

    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };


    public interface ConfigCallBack {

        void onConfigOk();

        void onConfigCancel();
    }



}
